package org.apak.frontend;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apak.db.RepairServiceDAO;
import org.apak.model.LoggedSubject;
import org.apak.model.ServiceNote;
import org.apak.model.ServiceOrder;

/**
 * Servlet implementation class ServiceOrderMenuServlet
 */
@WebServlet("/ServiceOrderMenuServlet")
public class ServiceOrderMenuServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private RepairServiceDAO dao = new RepairServiceDAO();
	private HttpSession session;
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		session = request.getSession(true);
		LoggedSubject loggedSubject = (LoggedSubject) session.getAttribute("loggedSubject");
		if(loggedSubject == null || !loggedSubject.isCustomer()) {
			RequestDispatcher dispatcher = request.getRequestDispatcher("error.jsp");
			dispatcher.forward(request, response);
		} else {
			ServiceOrder[] serviceOrderArray = dao.getServiceOrderByCustomerList(loggedSubject.getSubject_id());
			request.setAttribute("serviceOrderArray", serviceOrderArray);
			ArrayList<ServiceNote> employeeNotes = new ArrayList<ServiceNote>();
			ArrayList<ServiceNote> customerNotes = new ArrayList<ServiceNote>();
			for(int i = 0; i< serviceOrderArray.length; i++) {
				ArrayList<ServiceNote> list = (ArrayList<ServiceNote>) dao.getListOfEmployeeServiceNotes(serviceOrderArray[i].getService_order());
				for(ServiceNote note : list) {
					employeeNotes.add(note);
				}
				ServiceNote customerServiceNote = dao
						.getCustomerServiceNote(serviceOrderArray[i].getService_order());
				if(customerServiceNote != null) {
					customerNotes.add(customerServiceNote);
				}
			}
			request.setAttribute("employeeNotes", employeeNotes);
			request.setAttribute("customerNotes", customerNotes);
			if(serviceOrderArray != null) {
				boolean[] invoiceArray = new boolean[serviceOrderArray.length] ;
				for(int i = 0; i < serviceOrderArray.length; i++)
					if(dao.getInvoiceByServiceOrder(Integer.toString(serviceOrderArray[i].getService_order())) != null)
						invoiceArray[i] = true;
					else
						invoiceArray[i] = false;
				request.setAttribute("invoiceArray", invoiceArray);
			}		
			RequestDispatcher dispatcher = request.getRequestDispatcher("serviceOrderMenu.jsp");
			dispatcher.forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
